package com.sgb.kjwl.viewmodel.home;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.shopping.SearchTagEntity;
import com.sgb.kjwl.model.entity.user.GoodsListEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.utils.LocationUtil;
import com.sgb.kjwl.view.ui.activity.home.SearchShopFirstActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SearchShopFirstModel extends BaseViewModel {
    private SearchShopFirstActivity activity;
    private HomeRepertory repertory;
//    public Map<String, Object> map = new ArrayMap<>();
//    String latitude, longitude;
    int mPageIndex;

    public SearchShopFirstModel(SearchShopFirstActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void searchCategory() {
        NewAPIManager.getInstance().getMaterialAPI().getPopularSearchTerms("40", "1").enqueue(new Callback<BaseEntity<ListResponse<List<SearchTagEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<SearchTagEntity>>>> call, Response<BaseEntity<ListResponse<List<SearchTagEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    activity.updateDataTag(null);
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.updateDataTag(response.body().getData().getList());
                } else {
                    activity.updateDataTag(null);
                    MToast.showToast(activity, response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<SearchTagEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                activity.updateDataTag(null);
            }
        });
    }

    private Map<String, Object> goodsDownMap(boolean isLoadMore) {
        Map<String, Object> map = new HashMap<>();
        mPageIndex = isLoadMore ? mPageIndex + 1 : 1;
        map.put("pageNum", mPageIndex);
        map.put("pageSize", 10);
        if (LocationUtil.isOpenLocation(activity) && LocationUtil.currentLocationIsValid()) {
            map.put("lon", String.valueOf(LocationUtil.getCurrentLongitude()));
            map.put("lat", String.valueOf(LocationUtil.getCurrentLatitude()));
        } else {
            map.put("lon", "0");
            map.put("lat", "0");
        }
        map.put("areaCode", repertory.getAreaCode());
        map.put("distanceSort", "0");
        map.put("timeSort", "2");
        map.put("priceSort", "0");
        return map;
    }

    /**
     * 1.4.6 精选推荐
     *
     * @param
     */
    public void getGoodsList(boolean isLoadMore) {
        DialogHelper.showProgressDialog(activity, null, "获取信息中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getGoodsAPI().getGoodsList(goodsDownMap(isLoadMore)).enqueue(new Callback<BaseEntity<ListResponse<List<GoodsListEntity>>>>() {
            @Override
            public void onResponse(@NotNull Call<BaseEntity<ListResponse<List<GoodsListEntity>>>> call, @NotNull Response<BaseEntity<ListResponse<List<GoodsListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.updateData(response.body().getData().getList(), (response.body().getData().getCurrentPage() < response.body().getData().getTotal()));
                } else {
                    if(isLoadMore){
                        MToast.showToast(activity, "加载失败，请稍后重试");
                    }
                }
            }

            @Override
            public void onFailure(@NotNull Call<BaseEntity<ListResponse<List<GoodsListEntity>>>> call, @NotNull Throwable t) {
                if(isLoadMore){
                    MToast.showToast(activity, "加载失败，请稍后重试");
                }
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
